package com.jy.rhin.support.interceptor.convertor;

import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.apache.cxf.helpers.DOMUtils;
import org.oasis_open.docs.wsn.b_2.Notify;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.wsaddressing.W3CEndpointReference;
import java.util.Collections;
import java.util.List;

@Service("AuditInfoConverter4TerminologyDocumentNotify")
public class AuditInfoConverter4TerminologyDocumentNotify extends BaseAuditInfoConverter<Notify, Notify> {

    public AuditInfoConverter4TerminologyDocumentNotify() {
        super(EventActionCodeEnum.EXECUTE,
                ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH,
                EventTypeCodeEnum.DOCUMENT_RESUME_NOTIFY,
                true,
                ParticipantObjectIDTypeCodeEnum.URI,
                ParticipantObjectTypeCodeRoleEnum.SUBSCRIBER,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-subandpub-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(Notify request, Notify response) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(Notify request, Notify response) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(Notify request, Notify response) {
        return new AuditParticipantUserDesc("documents-subandpub-service", ServiceCodeEnum.DOCUMENTS_SUBSCRIBE_PUBLISH.getName());
    }

    @Override
    String getObjectId(Notify request, Notify response) {
        return "";
    }

    private String getWSAAddress(W3CEndpointReference ref) {
        Element element = DOMUtils.createDocument().createElement("elem");
        ref.writeTo(new DOMResult(element));
        NodeList nl = element.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
        if (nl != null && nl.getLength() > 0) {
            Element e = (Element) nl.item(0);
            return DOMUtils.getContent(e).trim();
        }
        return null;
    }
    
    @Override
	public boolean isMatch(String method, Object data) {
    	if(data instanceof Notify){
			Notify notify = (Notify)data;
			return "NotificationConsumerPortImpl.notify".equals(method) && ("rhin:documentEntry".equals(notify.getNotificationMessage().get(0).getTopic().getDialect())
					|| "rhin:documentReference".equals(notify.getNotificationMessage().get(0).getTopic().getDialect()) 
					);
		}
		return false;
	}

}
